<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" class="mr-5" placeholder="请输入昵称" suffix-icon="el-icon-search"
                v-model="nickname"></el-input>
      <el-input style="width: 200px" class="mr-5" placeholder="请输入邮箱" suffix-icon="el-icon-message"
                v-model="email"></el-input>
      <el-input style="width: 200px" placeholder="请输入手机号" suffix-icon="el-icon-phone"
                v-model="phonenumber"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reSet">重置</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" plain @click="openDialog"><i class="el-icon-circle-plus-outline"
                                                             style="margin-right: 5px"/>新增
      </el-button>
      <el-button type="danger" plain @click="batchDelete"><i class="el-icon-remove-outline"
                                                             style="margin-right: 5px"/>批量删除
      </el-button>
      <el-upload action="http://localhost:9876/user/import" accept="xlsx" :on-success="handelImport"
                 style="display: inline-block; margin-left: 10px">
        <el-button type="info" plain><i class="el-icon-download" style="margin-right: 5px"/>导入</el-button>
      </el-upload>
      <el-button type="warning" plain @click="excelExport" style="margin-left: 10px"><i class="el-icon-upload2"
                                                                                        style="margin-right: 5px"/>导出
      </el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column prop="userId" label="用户编号" width="80"></el-table-column>
      <el-table-column prop="userName" label="用户名称" width="120"></el-table-column>
      <el-table-column prop="nickName" label="用户昵称" width="120"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="phonenumber" label="手机号码"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="info" plain class="el-icon-menu" @click="selectRole(scope.row)"> 设置角色</el-button>
          <el-button type="success" plain class="el-icon-edit" @click="openEdit(scope.row)"> 修改</el-button>
          <el-button type="danger" plain class="el-icon-remove-outline" @click="handleRemove(scope.row.userId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form label-width="80px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名">
              <el-input v-model="from.userName" placeholder="请输入用户名" maxlength="30"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="昵称">
              <el-input v-model="from.nickName" placeholder="请输入用户昵称" maxlength="30"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-select v-model="from.sex" placeholder="请选择性别">
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话">
              <el-input v-model="from.phonenumber" placeholder="请输入用户电话" maxlength="11"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="邮箱">
              <el-input v-model="from.email" placeholder="请输入用户邮箱" maxlength="30"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="from.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="角色分配" :visible.sync="roleDialogVis" width="30%">
      <el-tree
          :props="props"
          :data="roleData"
          show-checkbox
          node-key="roleId"
          ref="tree"
          :default-expanded-keys="expends"
          :default-checked-keys="checks">
         <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>{{ data.roleName }}</span>
         </span>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveUserRole">确 定</el-button>
        <el-button @click="roleDialogVis = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "User",
  data() {
    return {
      tableData: [],
      userIds: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      from: {},
      nickname: "",
      email: "",
      phonenumber: "",
      dialogFormVisible: false,
      headerBg: "headerBg",
      title: "",
      roleDialogVis: false,
      userId: 0,
      roleData: [],
      props: {
        label: 'name',
      },
      expends: [],
      checks: [],
      options: [{
        value: '0',
        label: '男'
      }, {
        value: '1',
        label: '女'
      }, {
        value: '2',
        label: '未知'
      }],
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/user/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          nickName: this.nickname,
          email: this.email,
          phonenumber: this.phonenumber
        }
      }).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      })
    },
    selectRole(user) {
      this.userId = user.userId;
      // 请求角色数据
      this.request.get("/sys-role").then(res => {
        if (res.code === 200) {
          this.roleData = res.data;
          this.expends = this.roleData.map(v => v.roleId);
        }
      })
      // 获取已保存的数据
      this.request.get("/user/userRole/" + this.userId).then(res => {
        if (res.code === 200) {
          this.checks = res.data;
          this.roleDialogVis = true;
        }
      })
    },
    saveUserRole() {
      this.request.post("/user/userRole/" + this.userId, this.$refs.tree.getCheckedKeys()).then(res => {
        if (res.code === 200) {
          this.$message.success("绑定成功");
          this.roleDialogVis = false;
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    openDialog() {
      this.dialogFormVisible = true;
      this.from = {};
      this.title = "添加用户";
    },
    submitForm() {
      this.request.post("/user", this.from).then(res => {
        this.dialogFormVisible = false;
        if (res.code === 200) {
          this.$message.success("保存成功");
          this.load();
        } else {
          this.$message.error("保存失败");
        }
      })
    },
    openEdit(row) {
      this.from = row;
      this.dialogFormVisible = true;
      this.title = "修改用户";
    },
    handleRemove(id) {
      this.$confirm('是否确认删除用户编号为"' + id + '"的数据项？').then(() => {
        this.request.delete("/user/" + id).then(res => {
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.load();
          } else {
            this.$message.error("删除失败");
          }
        })
      })
    },
    handleSelectionChange(val) {
      console.log(val);
      this.userIds = val;
    },
    batchDelete() {
      let ids = this.userIds.map(v => v.userId);
      this.$confirm('是否确认删除这些用户').then(() => {
        this.request.post("/user/batchDelete", ids).then(res => {
          if (res.code === 200) {
            this.$message.success("批量删除成功");
            this.load();
          } else {
            this.$message.error("删除失败");
          }
        })
      })
    },
    excelExport() {
      window.open("http://localhost:9876/user/export")
    },
    handelImport() {
      this.$message.success("导入成功");
      this.load();
    },
    reSet() {
      this.nickname = "";
      this.phonenumber = "";
      this.email = "";
      this.load();
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    }
  }
}
</script>

<style>
.headerBg {
  background-color: #eee !important;
}
</style>
